﻿using System;
using System.Drawing;
using FSky;
using System.Windows.Forms; // 确保引用了System.Windows.Forms命名空间
namespace idea0
{
    public class Class2
    {
        public int X, Y; // 玩家坐标
        public int W = 60, H = 60;
        public const int cellSize = 60;
        public int Life = 3; // 生命值
        public int BombPlusCount = 1; // 增加炸弹次数的计数
        public DateTime CoolingEndTime = DateTime.MinValue;//冷却结束时间变量
        public Class1 blackWalls;
        public int PlusEffectCount = 0; // 增强炸弹威力的次数
                                        // public int BombPlusCount = 0; // 新增变量
        public List<Bomb> Bombs = new List<Bomb>(); // 玩家拥有的炸弹列表
        public DateTime LastBombTime = DateTime.MinValue; // 记录上一次放置炸弹的时间
        public System.Drawing.Image PlayerImage { get; set; }
        public (int, int) GridPosition => ((X - W / 2) / cellSize, (Y - H / 2) / cellSize);
        protected World world;

        public Class2(Class1 blackWalls, World world)
        {
            this.blackWalls = blackWalls;
            this.world = world;
            InitializePosition();
            string a3Path = Path.Combine(System.Windows.Forms.Application.StartupPath, "gif", "player.gif");
            PlayerImage = System.Drawing.Image.FromFile(a3Path);
        }


        private void InitializePosition()
        {
            Random rand = new Random();
            int x, y;
            do
            {
                x = rand.Next(0, 25);
                y = rand.Next(0, 13);
            } while (blackWalls.GetCellValue(x, y) == 1);

            X = x * cellSize + cellSize / 2;
            Y = y * cellSize + cellSize / 2;
        }

        public void KeyPress(char a)
        {
            int dx = 0, dy = 0;
            switch (a)
            {
                case 'w':
                    dy = -cellSize;
                    break;
                case 's':
                    dy = cellSize;
                    break;
                case 'a':
                    dx = -cellSize;
                    break;
                case 'd':
                    dx = cellSize;
                    break;
            }

            if (CheckMove(dx, dy))
            {
                X += dx;
                Y += dy;
            }
        }

        public bool CheckMove(int dx, int dy)
        {
            int newX = X + dx;
            int newY = Y + dy;

            int gridX = (newX - W / 2) / cellSize;
            int gridY = (newY - H / 2) / cellSize;

            if (gridX < 0 || gridX >= 25 || gridY < 0 || gridY >= 13)
                return false;

            if (blackWalls.GetCellValue(gridX, gridY) == 1)
                return false;

            if (this.world.IsBombPresent(gridX, gridY))
                return false;

            return true;
        }
        public void PlaceBomb(World world)
        {
            int x = GridPosition.Item1;
            int y = GridPosition.Item2;

            if (world.IsBombPresent(x, y))
            {
                return;
            }

            if (BombPlusCount > 0)
            {
                Bomb bomb = new Bomb(x, y);
                bomb.BombOwner = this; // 设置炸弹所有者
                Bombs.Add(bomb);
                world.Bombs.Add(bomb);
                BombPlusCount--;
            }
            else if (DateTime.Now - LastBombTime >= TimeSpan.FromSeconds(3))
            {
                Bomb bomb = new Bomb(x, y);
                bomb.BombOwner = this; // 设置炸弹所有者
                Bombs.Add(bomb);
                world.Bombs.Add(bomb);
                LastBombTime = DateTime.Now;
            }
        }
        public void PickUpPowerUp(int x, int y)
        {
            string powerUpType = world.WhitePieces.GetPowerUpType(x, y);
            switch (powerUpType)
            {
                case "lifeplus":
                    Life++;
                    break;
                case "bombplus":
                    BombPlusCount++;
                    break;
            }
            world.WhitePieces.WhiteBoard[y, x] = 0;
        }
        public void Update(World world)
        {
            // 检查是否需要恢复BombPlusCount
            if (BombPlusCount == 0 && DateTime.Now - LastBombTime >= TimeSpan.FromSeconds(3))
            {
                BombPlusCount = 1;
            }
            // 其他更新逻辑
        }
        public void Draw(Graphics g)
        {
            g.DrawImage(PlayerImage, X - W / 2, Y - H / 2, W, H);
        }
    }
}